package com.gcsoftware.web.service;

import com.gcsoftware.utils.MapUtils;
import com.gcsoftware.utils.log.InvokeLog;
import com.gcsoftware.utils.log.LogUtils;
import com.gcsoftware.web.beans.T_CON_DSITEM_IPARAMS;
import com.gcsoftware.web.mapper.T_GW_DSITEMMAPPER;
import com.gcsoftware.web.beans.T_GW_DSITEM;
import com.gcsoftware.utils.GenerateIDs;
import com.gcsoftware.web.mapper.T_GW_INTERFMAPPER;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qs on 2018/3/14.
 */
@Service
public class T_GW_DSITEM_SERVICE {
    @Autowired
    private T_GW_DSITEMMAPPER mapper ;

    @Autowired
    private T_GW_INTERFMAPPER interfMapper;

    public List<T_GW_DSITEM> queryList(String c_DS_ITEM_NAME) {
        return mapper.queryForList(c_DS_ITEM_NAME);
    }

    public int getMaxDsiId() {
        String c_DS_ITEM_ID = mapper.getMaxDsiId();
        int maxId = 0;
        if(StringUtils.isNotBlank(c_DS_ITEM_ID)) {
            maxId = Integer.valueOf(c_DS_ITEM_ID.replace(GenerateIDs.ID_TYPE_DSITEM_ID,""));
        }
        return maxId;
    }

    public T_GW_DSITEM getGwDsiItem( String c_DS_ITEM_ID ,  String c_DS_ITEM_NAME){
        return mapper.getGwDsiItem(c_DS_ITEM_ID,c_DS_ITEM_NAME);
    }

    @InvokeLog(name = "insert dsi", description = "新增敏感项",logType = LogUtils.LOG_TYPE_OPERATE)
    public MapUtils insertDsItem(T_GW_DSITEM dsitem) {
        if (GenerateIDs.getMaxDsItemId() == 0) {
            GenerateIDs.setMaxDsItemId(getMaxDsiId());
        }
        dsitem.setC_DS_ITEM_ID(GenerateIDs.GenerateId(GenerateIDs.ID_TYPE_DSITEM_ID));
        if (null!= mapper.getGwDsiItem(dsitem.getC_DS_ITEM_ID(),null)) {
            GenerateIDs.setMaxDsItemId(0);
            insertDsItem(dsitem);
        }

        if (null != mapper.getGwDsiItem(null, dsitem.getC_DS_ITEM_NAME())) {
            return MapUtils.error("存在相同的安全项名称");
        }
        return  1 == mapper.insertDsItem(dsitem) ? MapUtils.ok() : MapUtils.error("新增安全项失败！");
    }

    @InvokeLog(name = "update dsi", description = "修改敏感项",logType = LogUtils.LOG_TYPE_OPERATE)
    public MapUtils updateDsItem(T_GW_DSITEM dsitem){
        T_GW_DSITEM dsi = mapper.getGwDsiItem(null, dsitem.getC_DS_ITEM_NAME());
        if (null != dsi && !dsi.getC_DS_ITEM_ID().equals(dsitem.getC_DS_ITEM_ID())) {
            return MapUtils.error("存在相同的安全项名称");
        }
        return 1 == mapper.updateDsItem(dsitem) ? MapUtils.ok() : MapUtils.error("修改安全项失败！");
    }

    @InvokeLog(name = "delete dsi", description = "删除敏感项",logType = LogUtils.LOG_TYPE_OPERATE)
    public int deleteDsItem(String c_DS_ITEM_ID) {
        return mapper.deleteDsItem(c_DS_ITEM_ID);
    }

    public List<T_CON_DSITEM_IPARAMS> showInterfParams(String c_DS_ITEM_ID, String c_INTERF_ID) {
        return interfMapper.getIParamsRsp(c_DS_ITEM_ID,c_INTERF_ID);
    }

    @Transactional
    public MapUtils saveDsiIparams(String c_DS_ITEM_ID,List<T_CON_DSITEM_IPARAMS> list) {
        try {
            mapper.deleteDsIParams(c_DS_ITEM_ID);
            /*for (T_CON_DSITEM_IPARAMS cdi : list) {
                mapper.insertDsIparams(cdi);
            }*/
            List<T_CON_DSITEM_IPARAMS> lst = new ArrayList<>();
            for (T_CON_DSITEM_IPARAMS cdi : list) {
                if (1 == cdi.getIfChecked()) lst.add(cdi);
            }
            if (CollectionUtils.isNotEmpty(lst))
                mapper.batchInsertDsIparams(lst);
            return MapUtils.ok("配置安全项元数据成功");
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return MapUtils.error("配置安全项元数据失败");
        }
    }

}
